// pages/reservation/index.js
const App = getApp();
Page({
  /**
   * 组件的初始数据
   */
  data: {
    list: [],
    i: '-1',

    expense: '',
    deposit: '',
    clerk_id: '',
    shop_id: '',
    Cname: '',
    mobileN: '',
    multiArray: [

    ],
    demoArray: [],
    //实际显示值
    multiIndex: [0, 0],
    //临时变量
    demoIndex: [0, 0],
    timeList: [],
    dateList: [],
    date: '',
    hour: '',
    sign: '',
    subscribe_name:'',
    mobile:''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let _this = this;
    _this.getCardList();
    _this.getTime()
  },
  getTime() {
    let _this = this;
    App._get('card/fixedTime', {}, function (result) {
      const arrs = []
      const arr = result.data.date;
      const time = JSON.parse(JSON.stringify(result.data.time));
      const times = []
      time.forEach(item => {
        times.push(item.time);
        return
      });
      arrs.push(arr)
      arrs.push(times)
      _this.setData({
        timeList: result.data.time,
        dateList: result.data.date,
        multiArray: arrs,
        demoArray: arrs
      });
    });

  },
  //修改过程中取消修改
  cancelAddr() {
    var arr = JSON.parse(JSON.stringify(this.data.multiArray));
    var index = JSON.parse(JSON.stringify(this.data.multiIndex));
    this.setData({
      demoArray: arr,
      demoIndex: index
    })
  },
  //地址选择器改变
  bindMultiPickerColumnChange(e) {
    var _this = this
    var value = e.detail.value;
    var column = e.detail.column;
    var demoArray = this.data.demoArray;
    var demoIndex = this.data.demoIndex;
    demoIndex[column] = value;
    var timeIndex = demoIndex[1]
    var dateIndex = demoIndex[0]
    _this.setData({
      demoArray: demoArray,
      demoIndex: demoIndex,
      date: _this.data.dateList[dateIndex],
      hour: _this.data.timeList[timeIndex].time,
      sign: _this.data.timeList[timeIndex].sign
    })
  },
  //确定选中的地址
  bindMultiPickerChange(e) {
    var arr = JSON.parse(JSON.stringify(this.data.demoArray));
    var index = JSON.parse(JSON.stringify(this.data.demoIndex));
    this.setData({
      multiArray: arr,
      multiIndex: index
    })
  },
  userNameInputAction (options) {
    //获取输入框输入的内容
    let value = options.detail.value;
    this.setData({
      subscribe_name:value
    })
    console.log("输入框输入的内容是 " + value)
  },

  userPhoneInputAction (options) {
    //获取输入框输入的内容
    let value = options.detail.value;
    this.setData({
      mobile:value
    })
    console.log("输入框输入的内容是 " + value)
  },

  // 创建卡座订单
  createOrder() {
    let _this = this;
    App._post_form('card/createOrder', {
      shop_id:_this.data.shop_id,
      clerk_id:_this.data.clerk_id,
      expense:_this.data.expense,
      pay_amount:_this.data.deposit,
      date:_this.data.date,
      hour:_this.data.hour,
      sign:_this.data.sign,
      subscribe_name:_this.data.subscribe_name,
      mobile:_this.data.mobile,
      user_id:wx.getStorageSync('user_id')
    }, function (result) {
         wx.setStorageSync('card_id', result.data);
          wx.navigateTo({
            url: '/pages/reserve/index',
          })
    });
  },

  // 获取卡座列表
  getCardList() {
    let _this = this;
    App._get('card/cardList', {}, function (result) {
      _this.setData({
        list: result.data.data
      });
    });
  },
  /**
   * 组件的方法列表
   */
  // methods: {
  changeKey(e) {
    let _this = this;
    const selectId = e.currentTarget.dataset.id;
    _this.setData({
      i: selectId,
      clerk_id: e.currentTarget.dataset.cid,
      shop_id: e.currentTarget.dataset.sid
    })
    _this.data.list.forEach(item => {
      if (item.clerk_id == _this.data.clerk_id) {
        _this.setData({
          expense: item.expense,
          deposit: item.deposit,
          Cname: item.real_name,
          mobileN: item.mobile
        })
      }
    });
  },
  bindTimeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      time: e.detail.value
    })
  },
  reserve() {
    var _this=this;
    const detailList={
        shop_id:_this.data.shop_id,
        clerk_id:_this.data.clerk_id,
        expense:_this.data.expense,
        pay_amount:_this.data.deposit,
        date:_this.data.date,
        hour:_this.data.hour,
        sign:_this.data.sign,
        subscribe_name:_this.data.subscribe_name,
        mobile:_this.data.mobile,
        Cname: _this.data.Cname,
        mobileN: _this.data.mobileN
    }
    wx.setStorageSync('detailList', detailList);
    _this.createOrder()
  }

  // }
  // }
})